//index.js
//获取应用实例
var util = require('../../utils/util.js');

const app = getApp()
Page({
  data: {
    navbar: ['待出库', '待配送', '配送中'],
    currentTab: 0,
    date: '',
    date2: '',
    orderInfo: '',
    ids: '',
  
  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var token = app.globalData.token
    var that = this
    var ids = new Array()
    console.log("our token is:" + token)
    if (this.data.currentTab == 0) {
      that.getOrder(app.globalData.waitChu)
    } else if (this.data.currentTab == 1) {
      console.log(app.globalData.zhengChu)
      that.getOrder(app.globalData.zhengChu)
    } else if (this.data.currentTab == 2) {
      console.log(app.globalData.waitChu)
      that.getOrder(app.globalData.peisongIng)
    }
    
  },

  //事件处理函数
  navbarTap: function (e) {
    var that = this;
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    if (e.currentTarget.dataset.idx == 0) {
      that.getOrder(app.globalData.waitChu)
    } else if (e.currentTarget.dataset.idx == 1) {
      console.log(app.globalData.zhengChu)
      that.getOrder(app.globalData.zhengChu)
    } else if (e.currentTarget.dataset.idx == 2) {
      console.log(app.globalData.waitChu)
      that.getOrder(app.globalData.peisongIng)
    }
  },
  detail: function (e) {
    console.log(e.currentTarget.dataset.id)
    getApp().globalData.orderId = e.currentTarget.dataset.id
  },
  detailPage: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.replyPhone,
      success: function () {
        console.log("成功拨打电话")
      },
    })
  },
  testSubmit: function (e) {
    let formId = e.detail.formId;
    // this.dealFormIds(formId); //处理保存推送码
    console.log(formId)
    let type = e.detail.target.dataset.type;
    var token = app.globalData.token
    console.log("our token is:" + token)
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/formids/',
      data: {
        formId: formId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          console.log("提交成功")
        }
      }
    })
    //根据type的值来执行相应的点击事件
  },
  dealFormIds: function (formId) {
    let formIds = app.globalData.gloabalFomIds;//获取全局数据中的推送码gloabalFomIds数组
    if (!formIds) formIds = [];
    let data = {
      formId: formId,
      expire: parseInt(new Date().getTime() / 1000) + 604800 //计算7天后的过期时间时间戳
    }
    formIds.push(data);//将data添加到数组的末尾
    app.globalData.gloabalFomIds = formIds; //保存推送码并赋值给全局变量
  },
  getOrder: function (status) {
    var token = app.globalData.token
    var that = this
    var ids = new Array()
    console.log("our token is:" + token)
    wx.showLoading({
      title: '加载中....',
    })
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders?',
      data: {
        status: status,
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          wx.hideNavigationBarLoading()
          wx.stopPullDownRefresh()
          wx.hideLoading()
          console.log("获取到的为：" + res)
          that.setData({
            orderInfo: res.data.data.rows,
          })
          console.log(that.data.orderInfo)
          for (var i = 0; i < res.data.data.rows.length; i++) {
            ids[i] = res.data.data.rows[i].id
          }
          console.log("ids:" + ids)
          that.setData({
            ids: ids
          })
          console.log("ids:" + that.data.ids)

          var newTime = new Array()
          var newDate = new Array()
          
          for (var i = 0; i < res.data.data.rows.length; i++) {
            newDate = res.data.data.rows[i].updateTime
            let time = 'orderInfo[' + i + '].updateTime'
            
            that.setData({
              [time]: res.data.data.rows[i].updateTime.substring(11,16)
            })
            console.log(that.data.orderInfo[i].updateTime)
            let date = 'orderInfo[' + i + '].apartmentId'
            console.log(newDate.substring(5,10))
            that.setData({
              [date]: newDate.substring(5,10)
            })
            
            
          }
        } else if (res.statusCode == 401) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请前往个人中心验证账号',
            success: function (res) {
              if (res.confirm) {
                console.log('弹框后点取消')
                wx.switchTab({
                  url: '../person/person',
                  success(res) {
                    let page = getCurrentPages().pop();
                    if (page == undefined || page == null) {
                      return
                    }
                    page.onLoad();
                  }
                })
              } else {
                console.log('弹框后点取消')
              }
            }
          })
        }
      }
    })
  },
  orderOperation: function (e) {
    var that = this
    var token = app.globalData.token
    console.log("our Order id is:" + e.currentTarget.dataset.id)
    console.log("our token is:" + token)
    var status = app.globalData.peisongCompleted
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/' + e.currentTarget.dataset.id ,
      data: { 
        status: status
      },
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        
          wx.showToast({
            title: '配送成功',
            icon: 'success',
            duration: 1000
          })
          that.getOrder(app.globalData.peisongIng)
        
      }
    })
  },
  allComp: function () {
    var token = app.globalData.token
    console.log("our token is:" + token)
    var status = app.globalData.peisongCompleted
    var that = this
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/',
      data: {
        status: status,
        ids: that.data.ids
      },
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          wx.showToast({
            title: '一键配送完成',
            icon: 'success',
            duration: 1000
          })
          that.getOrder(app.globalData.peisongIng)
        }
      }
    })
  },
  onLoad: function (options) {
    var DATE = util.formatDate(new Date());
    var that = this
    console.log('当前日期为：' + DATE)
    this.setData({
      date: DATE,
      date2: DATE
    });
    setTimeout(function () {
      that.getOrder(app.globalData.waitChu)
    }, 2000)

  },

})
